package com.fanfanwang.ffw.app.db;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class ItotemContentProvider extends ContentProvider {

	private ItotemDB itotemDB;

	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/itotem.db";

	public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/itotem.db";

//	private static final int PICTURE_DB = 20;
//	private static final int PICTURE_SDCARD = 30;

	private static final int USER_INFO = 1000;

	private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		// 用户
		sURIMatcher.addURI(ItotemContract.AUTHORITY, ItotemContract.USER_URI, USER_INFO);
	}

	@Override
	public boolean onCreate() {
		itotemDB = new ItotemDB(getContext());
		itotemDB.open();
		return true;
	}

	@Override
	public String getType(Uri uri) {
		switch (sURIMatcher.match(uri)) {
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long result = -1;
		Uri resultUri = null;
		switch (sURIMatcher.match(uri)) {
//		case PICTURE_DB:
//			result = itotemDB.addDBPicture(values);
//			resultUri = buildResultUri(ItotemContract.PICTURE_DB_CONTENT_URI, result);
//			break;
		case USER_INFO:
			result = itotemDB.addUser(values);
			resultUri = buildResultUri(ItotemContract.USER_URI, result);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		if (result == 0 || result == -1) {
			return null;
		}
		getContext().getContentResolver().notifyChange(resultUri, null);
		return resultUri;

	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int result = 0;
		switch (sURIMatcher.match(uri)) {
		case USER_INFO:
			if (selection == null) {
				result = itotemDB.deleteAllUser();
			} else if (selection.equals("user_id")) {
				result = itotemDB.deleteUserByUserid(selectionArgs[0]);
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		return result;

	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		switch (sURIMatcher.match(uri)) {
//		case PICTURE_DB:
//			return itotemDB.getDBPicture(selection);

		case USER_INFO:
			if (projection != null) {
				if (selection.equals("user_id")) {
					return itotemDB.getUserById(projection[0]);
				}
			}
			return itotemDB.getUser();
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int result = 0;
		if (result > 0) {
			getContext().getContentResolver().notifyChange(uri, null);
		}

		return result;
	}

	@Override
	public int bulkInsert(Uri uri, ContentValues[] values) {
		int result = 0;
		switch (sURIMatcher.match(uri)) {
		default:
			result = super.bulkInsert(uri, values);
			break;
		}
		return result;
	}

	/**
	 * Build new result URI for given path.
	 * 
	 * @param path
	 *            resource's path.
	 * @param result
	 *            operation' result code.
	 * @return new instance of uri.
	 */
	private Uri buildResultUri(String path, long result) {
		final Uri.Builder builder = new Uri.Builder();
		builder.scheme(ItotemContract.SCHEME);
		builder.authority(ItotemContract.AUTHORITY);
		builder.path(path);
		builder.appendPath(String.valueOf(result));
		return builder.build();
	}

}
